import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:znmm/UI/A_Common/buttons/easy_button/easy_elevated_button.dart';
import 'package:znmm/UI/A_Common/custom_search_bar.dart';
import 'package:znmm/UI/CategoryPages/mag/category_mag.dart';
import 'package:znmm/UI/CategoryPages/view/category_list_view.dart';
import 'package:znmm/UI/ProductPages/mag/product_mag.dart';
import 'package:znmm/UI/ProductPages/model/product_model.dart';
import 'package:znmm/UI/ProductPages/show_product_view.dart';
import 'package:znmm/UI/utils/my_global.dart';
import 'package:znmm/config/app_color.dart';
import '../../../config/app_style.dart';
// import '../mag/home_mag.dart';
// import 'search_list_view.dart';

class SearchPage extends StatefulWidget {
  const SearchPage({
    Key? key,
  }) : super(key: key);

  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  late List<ProductModel> _pModelList = [];

  @override
  void initState() {
    super.initState();

    _pModelList = ProductMag().getSearchByName();

    ProductMag().reqAllProudut(
      callback: () {
        if (mounted) {
          setState(() {
            _pModelList = ProductMag().getSearchByName();
          });
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.backgroundColor,
      appBar: AppBar(
        backgroundColor: AppColor.whiteColor,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Navigator.of(context).pop(),
          color: Colors.black, // 修改返回按钮颜色为红色
        ),
        title: CustomSearchBar(
          fHeight: 44.h,
          onInputComplete: (value) {
            print(value);

            setState(() {
              _pModelList = ProductMag().getSearchByName(name: value);
            });
          },
        ),
      ),
      body: Padding(
          padding: const EdgeInsets.all(5),
          child: Column(
            //mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.fromLTRB(10, 0, 15, 0),
                child: Row(
                  children: [
                    Text(
                      "Products: ${_pModelList.length}",
                      overflow: TextOverflow.ellipsis,
                      style: h4Style,
                    ),
                    const Spacer(),
                    SizedBox(
                      height: 30,
                      child: EasyElevatedButton(
                        label: 'Show More',
                        isRounded: true,
                        color: Colors.black.withOpacity(0.8),
                        onPressed: () {
                          ProductMag().reqAllProudut(
                            showMore: true,
                            callback: () {
                              if (mounted) {
                                setState(() {
                                  _pModelList = ProductMag().getSearchByName();
                                });
                              }
                            },
                          );
                        },
                      ),
                    )
                  ],
                ),
              ),

              SizedBox(
                height: 5.h,
              ),
              const Divider(
                color: Colors.grey, // 设置线条颜色
                thickness: 0.5, // 设置线条粗细
              ),
              SizedBox(
                height: 5.h,
              ),
              Expanded(
                  child: Container(
                      color: Colors.white,
                      padding: const EdgeInsets.all(5),
                      child: _pModelList.isNotEmpty
                          ? CategoryListView(
                              prodcList: _pModelList,
                              onTap: (pModel) {
                                String sulgStr = pModel.slug;
                                print("sulgStr:$sulgStr");
                                CategoryMag().reqProductBySlugGraql(
                                  sulgStr,
                                  callback: (p0) {
                                    ProductModel pmodel = p0;
                                    Get.to(ShowProductView(
                                      product: pmodel,
                                    ));
                                  },
                                );
                              },
                            )
                          : MyGlobal.emptyWidget())),

              // SearchListView(
              //   prodcList: HomeMag.productList,
              //   //isHorizontal: false,
              //   //onTap: navigate,
              // ),
              SizedBox(
                height: 15.h,
              ),
            ],
          )),
    );
  }
}
